<template>
  <el-dialog :visible="isShow" title="分配权限" @close="closeEvent">
    <el-tree
      ref="tree"
      :data="treeData"
      node-key="id"
      :default-checked-keys="permIds"
      :props="{ label: 'name' }"
      show-checkbox
      check-strictly
    ></el-tree>
    <template #footer>
      <div style="text-align:center">
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermission } from '@/api/permission.js'
import { changTreeData } from '@/utils/index.js'
import { sysRoleGet, sysRolePut, sysRoleAssignPrem } from '@/api/setting.js'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      treeData: [],
      permIds: [],
      info: {},
      id: ''
    }
  },
  created () {
    this.getData()
  },
  methods: {
    closeEvent () {
      this.$emit('update:isShow', false)
      //   this.permIds=[],清空默认勾选
      this.$refs.tree.setCheckedKeys([])
    },
    // 获取所有权限点数据
    async getData () {
      const res = await sysPermission()
      this.treeData = changTreeData(res.data, '0')
    },
    // 获取角色详情,定义方法用于获取角色详情，通过详情设置el-tree设置
    async getRoleInfo (id) {
      this.id = id
      const res = await sysRoleGet(id)
      this.info = res.data
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log('角色详情', res)
    },
    // 点击确定时更新权限
    async submit () {
      // await sysRolePut({
      //   ...this.info,
      //   permIds: this.$refs.tree.getCheckedKeys()
      // })
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('更新权限成功')
      this.closeEvent()
      console.log('getCheckedKeys', this.$refs.tree.getCheckedKeys())
    }
  }
}
</script>

<style></style>
